package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.wx.coupon.vo.MyCouponVo;
import com.cskaoyan.bean.wx.order.bo.PageInfoBo;
import com.cskaoyan.service.wx.WxCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import com.cskaoyan.bean.wx.coupon.ExchangeCodeBo;
import com.cskaoyan.bean.wx.coupon.ReceiveCouponBo;
import com.cskaoyan.bean.wx.coupon.SelectListVoEach;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Name : WxCouponController.java
 * @Time : 2021/8/16 11:26
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */
/**
 * @Classname WxCouponController
 * @Description
 * @Date 2021/8/16 10:27
 * @Created by songweinan
 */

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    private WxCouponService wxCouponService;

    @RequestMapping("mylist")
    public BaseRespVo mylist(Integer status, PageInfoBo pageInfoBo){
        MyCouponVo mylist = wxCouponService.mylist(status, pageInfoBo);
        return BaseRespVo.ok(mylist);
    }

    @RequestMapping("list")
    public BaseRespVo list(PageInfoBo pageInfoBo) {
        MyCouponVo mylist = wxCouponService.list(pageInfoBo);
        return BaseRespVo.ok(mylist);
    }

    /**
     * 订单页面的优惠券列表
     * @param cartId
     * @param grouponRulesId
     * @return
     */
    @GetMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId,Integer grouponRulesId){

        List<SelectListVoEach> selectListVo =  wxCouponService.selectList(cartId,grouponRulesId);

        //无优惠券时selectListVo为null
        if(selectListVo == null){
            return BaseRespVo.fail("无可用优惠券");
        }
        return BaseRespVo.ok(selectListVo);
    }

    /**
     * 优惠券领取
     * @param receiveCouponBo
     * @return
     */
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody ReceiveCouponBo receiveCouponBo){

        Integer receiveStatus = wxCouponService.receive(receiveCouponBo);

        if(receiveStatus == 740){
            return BaseRespVo.fail("已到领取上限");
        }else if(receiveStatus == 741){
            return BaseRespVo.fail("优惠券已经领取完");
        }else if(receiveStatus == 742){
            return BaseRespVo.fail("未在领取时间");
        }
        return BaseRespVo.ok();
    }

    /**
     * 优惠券兑换码兑换
     * @param exchangeCodeBo
     * @return
     */
    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody ExchangeCodeBo exchangeCodeBo){

        Integer exchangeStatus = wxCouponService.exchange(exchangeCodeBo.getCode());

        if(exchangeStatus == 740){
            return BaseRespVo.fail("已到领取上限");
        }else if(exchangeStatus == 741){
            return BaseRespVo.fail("优惠券已经领取完");
        }else if(exchangeStatus == 742){
            return BaseRespVo.fail("未在领取时间");
        }

        return BaseRespVo.ok();
    }
}
